<?php
	// Author: Andy Marti
	// Datum: 12.03.2014
	// ----------------------------------------------------
	// Beschreibung Klasse:
	//   Die Klasse enthält alle Eigenschaften und Methoden eines einzelnen Users. 
	//   Diese Klasse hat als Properties Address, Contact, UserHistory, Job und Member. 
	//   Die Klasse User kann das Image verwalten.
	// ----------------------------------------------------
	// Änderungen:
	//
	// ----------------------------------------------------
	
	require_once 'base.php';
	require_once 'messages.php';
	require_once 'constants.php';
	require_once 'user_db.php';
	require_once 'address.php';
	require_once 'user_history.php';
	require_once 'contact.php';
	require_once 'job.php';
	
	/**
	 * Klasse User. Beschreibt einen einzelnen User.
	 * 
	 * @author Andy Marti
	 *
	 */
	class User extends Base {
		// ------------------------------------------------
		/**
		 * Die Eigenschaften der Klasse.
		 * 
		 * @var UserName
		 * @var Title
		 * @var FirstName
		 * @var LastName
		 * @var Birthday
		 * @var Age
		 * @var MemberFee
		 * @var Description
		 * @var ImagePath
		 * @var ImageWidth
		 * @var ImageHeight
		 * @var UserID
		 * @var ExternalUserID
		 * @var UserDb
		 * @var Address
		 * @var LastName
		 * @var Image
		 * @var Contact
		 * @var UserHistory
		 * @var Jobs
		 */
		// Public Variables.
		public $UserName = '';
		public $Title = '';
		public $FirstName = '';
		public $LastName = '';
		public $Birthday = '';
		public $Age = -1;
		public $Description = '';
		public $ImagePath = '';
		public $ImageWidth = -1;
		public $ImageHeigth = -1;
		public $State = -1;
		public $UserID = -1;
		public $AddressID = -1;
		public $ImageID = -1;
		public $ContactID = -1;
		public $ExternalUserID = -1;
		public $AccessRuleID = -1;
		public $AccessRule = '';
		// Private Variables.
		// Public Objects
		public $Address = NIL;
		public $Image = NIL;
		public $Contact = NIL;
		public $Jobs = NIL;
		public $Constants = NIL;
		public $Settings = NIL;
		// Private Objects.
		//
		
		// ------------------------------------------------
		// Public methodes.
		/**
		 * Get the user data from database.
		 * 
		 * @param External UserID $extuserid
		 */
		public function getUserData($extuserid, $loggedinuserid) {
			// Check basic and parameter.
			If ($this->checkInitial()) {
				If (empty($extuserid)) {
					$this->Messages->throwMessage($this->Messages->NumWrongUserId, '');
				}
				// Get user from UserDB object.
				$this->UserDb->getUser($this, $extuserid, $loggedinuserid);
				// Get Job data.
				$this->Jobs->getJobData($this->UserID);
				// Fill internal fields.
				$this->ExternalUserID = $extuserid;
			}
		}
		
		/**
		 * Set the user data into database.
		 * 
		 * 
		 */
		public function setUserData() {
			// Check basic and parameter.
			If ($this->checkInitial()) {
				// Set user in UserDB object.
				$this->UserDb->setUser($this);
			}
		}

		/**
		 * Set a new user data into database.
		 *
		 * @param External UserID $extuserid
		 */
		public function newUserData($extuserid) {
			// Check basic and parameter.
			If ($this->checkInitial()) {
				If (empty($extuserid)) {
					$this->Messages->throwMessage($this->Messages->NumWrongUserId, '');
				}
				// Set user in UserDB object.
				$this->UserDb->newUser($this, $extuserid);
				// Fill internal fields.
				$this->ExternalUserID = $extuserid;
			}
		}
		
		/**
		 * Set the user data into database.
		 *
		 * @param External UserID $extuserid
		 */
		public function setUserState() {
			// Check basic and parameter.
			If ($this->checkInitial()) {
				// Set user in UserDB object.
				$this->UserDb->setUserState($this, $extuserid);
			}
		}

		/**
		 * Set the Job state into database.
		 *
		 */
		public function setJobs() {
			// Check basic and parameter.
			If ($this->checkInitial()) {
				// Set Jobs in UserDB object.
				$this->UserDb->setJobs($this);
			}
		}
		
		public function isSuperAdmin() {
			return ($this->AccessRuleID == $this->Constants->getConst('ACL_SUPER_ADMIN'));
		}
		
		public function initUser() {
			// check basic.
			If ($this->checkInitial()) {
				// Set internal objects.
				$this->Address->Messages = $this->Messages;
				$this->Address->UserDb = $this->UserDb;
				$this->Contact->Messages = $this->Messages;
				$this->Contact->UserDb = $this->UserDb;
				$this->UserHistory->Messages = $this->Messages;
				$this->UserHistory->UserDb = $this->UserDb;
				$this->Jobs->Messages = $this->Messages;
				$this->Jobs->UserDb = $this->UserDb;
			}
		}
		
		// ------------------------------------------------
		// Contructor, Destructor.
		/**
		 * Construtor. 
		 * 
		 */
		public function __construct(){
			// Cresate internal objects.
			$this->Address = new Address();
			$this->Contact = new Contact();
			$this->UserHistory = new UserHistory();
			$this->Jobs = new Job();
		}
		
		/**
		 * Destructor.
		 *
		 */
		public function __destruct(){
			// Destroy internal objects.
			$this->Jobs = NIL;
			$this->UserHistory = NIL;
			$this->Contact = NIL;
			$this->Address = NIL;
		}
	}
?>